package service

import (
	sqlc "bgs/db/sqlc"
	"bgs/util"
	"context"
	"fmt"
)

type ListActivityWithStatPtrsOfStoreParam struct {
	StoreID         int64
	IsAll           bool
	ProcessStatuses []string
	Keyword         string
}

// ListActivityWithStatPtrsOfStore
func (s *ActivityServiceImp) ListActivityWithStatPtrsOfStore(ctx context.Context, q *sqlc.Queries, pagerPtr *util.Pager, ptr *ListActivityWithStatPtrsOfStoreParam) (total int64, ptrs []*sqlc.ListActivityItemOfStoreWithStatsByStoreIDRow, err error) {
	ts := pagerPtr.Ts
	storeID := ptr.StoreID
	isAll := ptr.IsAll
	processStatuses := ptr.ProcessStatuses
	keyword := ptr.Keyword
	isKeywordEmpty := util.IsEmpty(keyword)
	keywordLike := fmt.Sprintf("%%%s%%", keyword)
	asFeatureTypes := ActivitiesAsFeature

	total, err = q.CountActivityItemOfStoreWithStatsByStoreID(ctx, sqlc.CountActivityItemOfStoreWithStatsByStoreIDParams{
		AsFeatureTypes:  asFeatureTypes,
		Created:         ts,
		StoreID:         sqlc.NInt64FromInt64(storeID),
		IsAll:           isAll,
		ProcessStatuses: processStatuses,
		IsKeywordEmpty:  isKeywordEmpty,
		Keyword:         keywordLike,
	})
	if err != nil {
		return
	}

	if total == 0 {
		// 不用继续调用listfunc
		return
	}

	rows, err := q.ListActivityItemOfStoreWithStatsByStoreID(ctx, sqlc.ListActivityItemOfStoreWithStatsByStoreIDParams{
		Limit:           pagerPtr.Limit,
		Offset:          pagerPtr.Offset,
		AsFeatureTypes:  asFeatureTypes,
		Created:         ts,
		StoreID:         sqlc.NInt64FromInt64(storeID),
		IsAll:           isAll,
		ProcessStatuses: processStatuses,
		IsKeywordEmpty:  isKeywordEmpty,
		Keyword:         keywordLike,
	})
	if err != nil {
		return
	}
	ptrs = s.MapListActivityItemOfStoreWithStatsByStoreIDRowEntityToPtr(rows)
	return
}



// ListActivityWithStatPtrsOfStore
func (s *ActivityServiceImp) RankActivityItemOfStoreWithStatsOverOrderAmount(ctx context.Context, q *sqlc.Queries, pagerPtr *util.Pager, storeID int64) (total int64, ptrs []*sqlc.RankActivityItemOfStoreWithStatsOverOrderAmountByStoreIDRow, err error) {
	ts := pagerPtr.Ts
	asFeatureTypes := ActivitiesAsFeature

	total, err = q.CountRankActivityItemOfStoreWithStatsOverOrderAmountByStoreID(ctx, sqlc.CountRankActivityItemOfStoreWithStatsOverOrderAmountByStoreIDParams{
		AsFeatureTypes:  asFeatureTypes,
		Created:         ts,
		StoreID:         sqlc.NInt64FromInt64(storeID),
	})
	if err != nil {
		return
	}

	if total == 0 {
		// 不用继续调用listfunc
		return
	}

	rows, err := q.RankActivityItemOfStoreWithStatsOverOrderAmountByStoreID(ctx, sqlc.RankActivityItemOfStoreWithStatsOverOrderAmountByStoreIDParams{
		Limit:           pagerPtr.Limit,
		Offset:          pagerPtr.Offset,
		AsFeatureTypes:  asFeatureTypes,
		Created:         ts,
		StoreID:         sqlc.NInt64FromInt64(storeID),
	})
	if err != nil {
		return
	}
	ptrs = s.MapRankActivityItemOfStoreWithStatsOverOrderAmountByStoreIDRowEntityToPtr(rows)
	return
}
